home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2293 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  76 lines

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Callbacks using member functions
  5. Date: 16 Jan 1996 19:40:02 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4dguui$m7k@locutus.rchland.ibm.com>
  8. References: <4dgkke$8mf@knot.queensu.ca>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4dgkke$8mf@knot.queensu.ca>, 3mal5@qlink.queensu.ca (Lepage Marc A) writes:
  14. >I am writing a MOTIF application, and I wish to use classes for the 
  15. >various elements of my program.  MOTIF is C, and uses callbacks.  I want 
  16. >to install member functions as the callbacks, but g++ complains, I 
  17. >suspect because of the implicit 'this' parameter.  Is there a way to get 
  18. >this to work (ie, why can't the compiler straighten everything out)?
  19. >
  20. >I expect that I cannot use a member function as a callback, but wish to 
  21. >confirm this before I go ahead and start using friend functions.
  22. >
  23. >So on a related note, if I use friends, what is the best way to make sure 
  24. >I have access to the proper class instances, without the 'this' parameter?
  25.  
  26. This is precisely why the compiler can't do it for you.  Which instance,
  27. if any?  From the C++ FAQ:
  28.  
  29. ==============================================================================
  30.  
  31. Q112: How do I pass a ptr to member fn to a signal handler, X event callback,
  32.    etc?
  33.  
  34. Don't.
  35.  
  36. Because a member function is meaningless without an object to invoke it on, you
  37. can't do this directly (if The X Windows System was rewritten in C++, it would
  38. probably pass references to OBJECTS around, not just pointers to fns; naturally
  39. the objects would embody the required function and probably a whole lot more).
  40.  
  41. As a patch for existing software, use a top-level (non-member) function as a
  42. wrapper which takes an object obtained through some other technique (held in a
  43. global, perhaps).  The top-level function would apply the desired member
  44. function against the global object.
  45.  
  46. E.g., suppose you want to call Fred::memfn() on interrupt:
  47.  
  48.     class Fred {
  49.     public:
  50.       void memfn();
  51.       static void staticmemfn();    //a static member fn can handle it
  52.       //...
  53.     };
  54.  
  55.     //wrapper fn remembers the object on which to invoke memfn in a global:
  56.     Fred* object_which_will_handle_signal;
  57.     void Fred_memfn_wrapper() { object_which_will_handle_signal->memfn(); }
  58.  
  59.     main()
  60.     {
  61.       /* signal(SIGINT, Fred::memfn); */   //Can NOT do this
  62.       signal(SIGINT, Fred_memfn_wrapper);  //Ok
  63.       signal(SIGINT, Fred::staticmemfn);   //Also Ok
  64.     }
  65.  
  66. Note: static member functions do not require an actual object to be invoked, so
  67. ptrs-to-static-member-fns are type compatible with regular ptrs-to-fns (see ARM
  68. ["Annotated Reference Manual"] p.25, 158).
  69.  
  70. ==============================================================================
  71.  
  72.  
  73. Phil Staite, team OS/2
  74. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  75.  
  76.